package com.kun.portal.webframe.controller;

import com.kun.portal.webframe.po.CertificateFilePo;
import com.kun.portal.webframe.po.CertificateMgrPo;
import com.kun.portal.webframe.po.FlyMessagePo;
import com.kun.portal.webframe.service.FlightCrewMgrService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.datetime.DateFormatter;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Enumeration;
import java.util.List;

/**
 * Created by Administrator on 2018-07-17.
 * 机组人员管理
 */
@Controller
@RequestMapping({"/api/flightCrewMgr"})
public class FlightCrewMgrController {
    @Autowired
    private FlightCrewMgrService flightCrewMgrService;
    @InitBinder
    public void intDate(WebDataBinder dataBinder){
        dataBinder.addCustomFormatter(new DateFormatter("yyyy-MM-dd"));
    }
    @RequestMapping(
            value = {"/findAllCertificateMgrPo"},
            method = {RequestMethod.GET},
            produces = {"application/json"}
    )
    @ResponseBody
    public List<CertificateMgrPo>findAllCertificateMgrPo(){
        return flightCrewMgrService.findAllCertificateMgrPo();
    }
    @RequestMapping(
            value = {"/saveCertificate"},
            method = {RequestMethod.POST},
            produces = {"application/json"}
    )
    @ResponseBody
    public void saveCertificate(@RequestBody CertificateMgrPo certificateMgrPo){
        flightCrewMgrService.saveCertificate(certificateMgrPo);

    }
    @RequestMapping(
            value = {"/saveCertificateFile"},
            method = {RequestMethod.POST},
            produces = {"application/json"}
    )
    @ResponseBody
    public void saveCertificateFile(@RequestBody CertificateFilePo certificateFilePo){
        flightCrewMgrService.saveCertificateFilePo(certificateFilePo);
    }
    @RequestMapping(
            value = {"/checkCertificate"},
            method = {RequestMethod.GET},
            produces = {"application/json"}
    )
    @ResponseBody
    public List<CertificateMgrPo> checkCertificate(
            @RequestParam(required = false) String userCnName,
            @RequestParam(required = false) String idNumber){
        return flightCrewMgrService.checkCertificate(userCnName,idNumber);
    }
    @RequestMapping(
            value = {"/deleteCertificate"},
            method = {RequestMethod.POST},
            produces = {"application/json"}
    )
    @ResponseBody
    public void deleteCertificate(@RequestBody CertificateMgrPo certificateMgrPo){
                flightCrewMgrService.deleteCertificate(certificateMgrPo);
    }

    @RequestMapping(
            value = {"/deleteCertificateFile"},
            method = {RequestMethod.POST},
            produces = {"application/json"}
    )
    @ResponseBody
    public void deleteCertificateFile(@RequestBody CertificateFilePo certificateFliePo){
        flightCrewMgrService.deleteCertificateFile(certificateFliePo);
    }
    @RequestMapping(value="/upload",method= RequestMethod.POST)
    @ResponseBody
    public CertificateFilePo upload(HttpServletRequest request, @RequestParam(required = false) MultipartFile file) throws Exception {
        CertificateFilePo certificateFilePo=new CertificateFilePo();
            //上传文件路径
            String path = request.getServletContext().getRealPath("");
            //上传文件名
            path+="../certificateFile";
            String filename = file.getOriginalFilename();
            File filepath = new File(path,filename);
            //判断路径是否存在，如果不存在就创建一个
            if (!filepath.getParentFile().exists()) {
                filepath.getParentFile().mkdirs();
            }
            //将上传文件保存到一个目标文件当中
            file.transferTo(new File(path + File.separator + filename));
                certificateFilePo.setFileName(filename);
                certificateFilePo.setFileUrl("/flyMessage/"+filename);
            return  flightCrewMgrService.saveCertificateFilePo(certificateFilePo);
    }
}
